"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能-功能测试
Case Name   : 插件可以打开单独的Tab
Create At   : 2023/5/19
Owner       : n@ningyali
Description :
    1. 点击业务建模-报表设计，打开报表设计Tab页面
    2. 点击新建数据流创建一个数据流或修改已有的数据流
Expect      :
    1.【结果1】插件会打开一个“数据流详情”的Tab页面
History     :
"""
import os
import time
import unittest

from selenium.webdriver.common.by import By

from openGaussMigrate.common.base import Base
from openGaussMigrate.common.com_log import ComLog
from openGaussMigrate.common.com_node import Macro, Node
from openGaussMigrate.common.com_unit_action import ComUnitAction


class Install0012(unittest.TestCase):
    def setUp(self):
        self.log = ComLog(self.__class__.__name__)
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        web = Macro('WEB_INFO')
        self.driver = ComUnitAction(web.info['driver_path'], web.info['url'])
        self.driver.open()
        self.driver.login(web.info['user'], web.info['passwd'])
        self.log.info('setup run finished')
        self.data_stream_name = 'dk_install_0012'
        self.plugin_name = "base-ops"
        self.plugin_name_cn = "基础运维"
        self.plugin_path = os.path.join(os.getcwd(),
                                        'resources',
                                        'plugins',
                                        'base-ops-5.0.0-repackage.jar')

    @Base.retry(max_n=1)
    @Base.save_img_when_err(cls_name='Install0012')
    def test_case1(self):
        """插件可以打开单独的Tab"""
        self.log.info('前置操作：安装插件')
        if not self.driver.is_plugin_intalled(self.plugin_name):
            self.driver.install_plugin(self.plugin_name_cn, self.plugin_path)

        self.log.info('步骤1：点击业务建模-报表设计，打开报表设计Tab页面')
        menu_xpath = '//span[contains(@class,"arco-menu-title") ' \
                     'and text()="业务建模"]'
        sub_menu_xpath = '//span[contains(@class,"arco-menu-item-inner") ' \
                         'and text()="报表设计"]'
        self.driver.wait_click(menu_xpath)
        time.sleep(1)
        self.driver.wait_click(sub_menu_xpath)
        time.sleep(5)

        self.log.info('步骤2：点击新建数据流创建一个数据流或修改已有的数据流')
        js = "document.querySelector('.wujie_iframe').shadowRoot." \
             "querySelector('button[type]').click()"
        self.driver.exec_js(js)

        js = "document.querySelector('.wujie_iframe').shadowRoot." \
             "querySelector('.arco-select-view-icon').click()"
        self.driver.exec_js(js)

        js = "document.querySelector('.wujie_iframe').shadowRoot." \
             "querySelector('ul[role=\\'menu\\'] li').click()"
        self.driver.exec_js(js)

        js = "document.querySelector('.wujie_iframe').shadowRoot." \
             "querySelectorAll('.arco-cascader-panel-column')[1]." \
             "querySelector('li').click()"
        self.driver.exec_js(js)

        js = "document.querySelector('.wujie_iframe').shadowRoot." \
             "querySelectorAll('.arco-cascader-panel-column')[2]." \
             "querySelector('li').click()"
        self.driver.exec_js(js)

        js = "document.querySelector('.wujie_iframe').shadowRoot." \
             "querySelectorAll('.arco-cascader-panel-column')[3]." \
             "querySelector('li').click()"
        self.driver.exec_js(js)

        js = f"document.querySelector('.wujie_iframe').shadowRoot." \
            f"querySelector('input[placeholder=\\'请输入数据流名称\\']')." \
            f"value='{self.data_stream_name}';" \
            f"document.querySelector('.wujie_iframe').shadowRoot." \
            f"querySelector('input[placeholder=\\'请输入数据流名称\\']')." \
            f"dispatchEvent(new Event('input'));"
        self.driver.exec_js(js)

        js = "document.querySelector('.wujie_iframe').shadowRoot." \
             "querySelector('.arco-modal-footer button.arco-btn-primary')." \
             "click()"
        self.driver.exec_js(js)

        time.sleep(3)

    def tearDown(self):
        try:
            tab_xpath = '//span[@class="tag-link" and text()="报表设计"]'
            self.driver.wait_click(tab_xpath)
            time.sleep(1)

            js = "document.querySelector('.wujie_iframe').shadowRoot." \
                 "querySelector('.arco-icon-delete').parentElement.click()"
            self.driver.exec_js(js)

            js = "document.querySelector('.wujie_iframe').shadowRoot." \
                 "querySelectorAll('.arco-popconfirm-footer button')" \
                 "[1].click()"
            self.driver.exec_js(js)
            time.sleep(3)

            if self.driver.is_plugin_intalled(self.plugin_name):
                self.driver.uninstall_plugin(self.plugin_name)
        except Exception as e:
            self.log.error(str(e))
        finally:
            self.driver.close()
            self.log.info('teardown run finished')
